'''
Nicholas Swartzendruber

This will pull together the subreddit loader
and nltk score to figure out which subreddit will
be the best.
'''
from nltk.corpus import wordnet as wn

#The minimum similarity
sim_line = .75

word1 = 'baseball'

subreddit_file = 'subreddits.txt'

subreddits = []

with open(subreddit_file, 'r+') as f:
  while 1:
    line = f.readline()
    if not line:
      break
    tok = line.split()
    meaning = tok[0]
    
    if len(tok) != 1:
      meaning = tok[1]
      
    subreddits.append((tok[0], meaning))
    
    
similars = []

synsets1 = wn.synsets(word1)
group1 = [wn.synset(str(synset.name)) for synset in synsets1]

for reddit in subreddits:
  synsets2 = wn.synsets(reddit[1])
  group2 = [wn.synset(str(synset.name)) for synset in synsets2]
  
  sim2 = []
  for sseta in group1:
    for ssetb in group2:
      wup_similarity = sseta.wup_similarity(ssetb)
      
      if wup_similarity is not None and wup_similarity > sim_line:
        sim2.append({
           'wup':wup_similarity,
           'subreddit':reddit[0]
        })
  
  sim2 = sorted(sim2, key=\
  lambda item: item['wup'], reverse=True)
        
  if len(sim2) > 0:
    similars.append(sim2[0])
  
similars = sorted(similars, key=\
  lambda item: item['wup'], reverse=True)

for item in similars:
  print item['subreddit'], '- ', item['wup'], '\n'
  
  

